using System;
using UnityEngine;

namespace CGF
{
    public class HotfixLaunch : MonoBehaviour
    {
        public static HotfixLaunch instance;
        
        public event Action fixedUpdateAction;
        public event Action updateAction;
        public event Action lateUpdateAction;
        public event Action<bool> appFocusAction;
        public event Action<bool> appPauseAction;

        private static GameObject go;
        private static RootComponent rootComponent;

        //包内主程序通过反射调用
        public static async void Run()
        {
            if (!go)
            {
                GameObject prefab = await ResManager.Instance.LoadAsset<GameObject>("Prefabs/HotfixLaunch/" + nameof(HotfixLaunch));
                go = Instantiate(prefab);
                go.name = nameof(HotfixLaunch);
            }
        }

        private void Awake()
        {
            instance = this;
        }

        //主程序调用
        private async void Start()
        {
            if (Application.isPlaying)
            {
                DontDestroyOnLoad(gameObject);
            }

            Screen.sleepTimeout = SleepTimeout.NeverSleep;
            Application.runInBackground = true;
            Application.targetFrameRate = 60;
            Time.fixedDeltaTime = 0.03f;

            gameObject.AddComponent<MainThread>();
            
            //创建MainUI
            GameObject prefab = await ResManager.Instance.LoadAsset<GameObject>("Prefabs/HotfixLaunch/MainUI");
            Instantiate(prefab);
            ScreenHelper.InitScreenUISize(UILayerControl.instance.normalRoot.GetComponent<Canvas>());

            Debug.canLog = Application.isEditor || true; // CGFConfig.Instance.gameServer != EmGameServer.FormalServer;
            Debug.canLogWarning = Application.isEditor || true; // CGFConfig.Instance.gameServer != EmGameServer.FormalServer;
            Debug.canLogError = Application.isEditor || true; // CGFConfig.Instance.gameServer != EmGameServer.FormalServer;
            Debug.canLogToServer = true;

            UnityEngine.Debug.Log($"日志：canLog：{Debug.canLog}");

            //AttributeHelper.Init();

            //await DebugCanvas.Show();//显示帧率

            //IPHelper.Init();

            I18N.Init();

            rootComponent = new RootComponent();
            (rootComponent as IAwake).Awake();

            fixedUpdateAction += ComponentBase._FixedUpdate;
            updateAction += ComponentBase._Update;
            lateUpdateAction += ComponentBase._LateUpdate;
            appFocusAction += ComponentBase._AppFocus;
            appPauseAction += ComponentBase._AppPause;

            RootComponent.instance.AddComponent<GameLaunch>();
        }

        void FixedUpdate()
        {
            fixedUpdateAction?.Invoke();
        }

        void Update()
        {
            updateAction?.Invoke();
        }

        void LateUpdate()
        {
            lateUpdateAction?.Invoke();
        }

        void OnApplicationFocus(bool focus)
        {
            appFocusAction?.Invoke(focus);
        }

        void OnApplicationPause(bool pause)
        {
            appPauseAction?.Invoke(pause);
        }

        //主程序调用
        void OnDestroy()
        {
            fixedUpdateAction -= ComponentBase._FixedUpdate;
            updateAction -= ComponentBase._Update;
            lateUpdateAction -= ComponentBase._LateUpdate;
            rootComponent?.Destroy();
            instance = null;
        }
    }
}